package com.infinit.selfhelp.common.utils

import android.graphics.Color
import android.graphics.Paint
import android.text.Spannable
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.ImageSpan
import android.text.style.StyleSpan
import android.view.View
import android.widget.TextView

class TextViewSpannableUtils {
    companion object {
        var mClickableSpanCallBack: ClickableSpanCallBack? = null
        fun setSpannable(content: String, keyWord: String, resColorID: Int, textView: TextView, isUnderLine: Boolean = true) {
            val start = content.indexOf(keyWord) // 截取文字开始的下标
            val end = start + keyWord.length
            if (start < 0) {
                if (content.isNotEmpty()) {
                    textView.text = content
                } else {
                    textView.text = ""
                }
                return
            }
            val sp = SpannableString(content)
            sp.setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        // 点击后的操作
                        if (mClickableSpanCallBack != null) {
                            mClickableSpanCallBack?.onClick()
                        }
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        ds.isUnderlineText = isUnderLine
                    }
                },
                start,
                end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            if (isUnderLine) {
                sp.setSpan(StyleSpan(Paint.UNDERLINE_TEXT_FLAG), start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
                sp.setSpan(ForegroundColorSpan(resColorID), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
            textView.text = sp
            textView.movementMethod = LinkMovementMethod.getInstance()
        }

        fun setSpannable(content: String, keyWords: List<String>, listenerList: List<ClickableSpanCallBack>, resColorID: Int, textView: TextView) {
            val sp = SpannableString(content)
            keyWords.forEachIndexed { index, keyWord ->
                val start = content.indexOf(keyWord) // 截取文字开始的下标
                val end = start + keyWord.length
                if (start < 0) {
                    if (content.isNotEmpty()) {
                        textView.text = content
                    } else {
                        textView.text = ""
                    }
                    return
                }
                sp.setSpan(
                    object : ClickableSpan() {
                        override fun onClick(widget: View) {
                            // 点击后的操作
                            if (listenerList.size > index) {
                                listenerList[index].onClick()
                            }
                        }
                    },
                    start,
                    end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )
                sp.setSpan(StyleSpan(Paint.UNDERLINE_TEXT_FLAG), start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
                sp.setSpan(ForegroundColorSpan(resColorID), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
            textView.text = sp
            textView.movementMethod = LinkMovementMethod.getInstance()
        }

        fun setImageSpannable(content: String, textView: TextView, resourceId: Int) {
            val spannaString = SpannableString("$content 1")
            val drawableResource = textView.context.resources.getDrawable(resourceId)
            drawableResource.setBounds(0, 0, 40, 40)
            val imageSpan = ImageSpan(drawableResource)

            spannaString.setSpan(imageSpan, spannaString.length - 1, spannaString.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
            val clickableSpan = object : ClickableSpan() {
                override fun onClick(widget: View) {
                }

                override fun updateDrawState(ds: TextPaint) {
                    ds.isUnderlineText = false
                }
            }
            spannaString.setSpan(clickableSpan, spannaString.length - 1, spannaString.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
            textView.text = spannaString
            textView.movementMethod = LinkMovementMethod.getInstance()
        }

        fun setLastSpannable(content: String, keyWord: String, resColorID: Int, textView: TextView, isUnderLine: Boolean = true, click: () -> Unit) {
            val start = content.lastIndexOf(keyWord) // 截取文字开始的下标
            val end = start + keyWord.length
            if (start < 0) {
                if (content.isNotEmpty()) {
                    textView.text = content
                } else {
                    textView.text = ""
                }
                return
            }
            val sp = SpannableString(content)
            sp.setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        // 点击后的操作
                        if (mClickableSpanCallBack != null) {
                            mClickableSpanCallBack?.onClick()
                        }
                        click()
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        ds.isUnderlineText = isUnderLine
                    }
                },
                start,
                end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            if (isUnderLine) {
                sp.setSpan(StyleSpan(Paint.UNDERLINE_TEXT_FLAG), start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
                sp.setSpan(ForegroundColorSpan(resColorID), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
            textView.text = sp
            textView.highlightColor = Color.TRANSPARENT
            textView.movementMethod = LinkMovementMethod.getInstance()
        }
    }

    interface ClickableSpanCallBack {
        fun onClick()
    }
}
